package com.kcsj.dao;
/**
 * @author 李鑫元
 * 4th Dec. 2020
 */
import com.kcsj.dao.User.UserDao;
import com.kcsj.domain.user.LoginLog;
import com.kcsj.domain.user.User;
import com.kcsj.util.Condition;
import com.kcsj.util.JdbcHelper;

import java.sql.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class LoginLogDao  {
    public static LoginLogDao loginLogDao = new LoginLogDao();
    public static LoginLogDao getInstance() {return loginLogDao;}

    public Collection<LoginLog> findAll(List<Condition> conditionList, Connection conn) throws SQLException {
        ArrayList<LoginLog> loginLogs = new ArrayList<LoginLog>();
        //预编译方式进行查询
        String s = "select * from LoginLog";
        if (conditionList != null){
            s += Condition.toWhereClause(conditionList);
        }

        PreparedStatement preparedStatement = conn.prepareStatement(s);
        ResultSet resultSet = preparedStatement.executeQuery();
        //将查询到的数据存到departments集合中
        while (resultSet.next()){
            int id = resultSet.getInt(1);
            int user_id = resultSet.getInt(2);
            String IP = resultSet.getString(3);
            String loginLocation = resultSet.getString(4);
            String clientSystem = resultSet.getString(5);
            String HTTPProtocol = resultSet.getString(6);
            String visitBrowser = resultSet.getString(7);
            Timestamp timestamp = resultSet.getTimestamp(8);
            User user = UserDao.getInstance().find(user_id, conn);
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LoginLog loginLog = new LoginLog(id,user,IP,loginLocation,clientSystem,HTTPProtocol,visitBrowser,
                    timestamp.toLocalDateTime().format(dtf));
            loginLogs.add(loginLog);
        }
        JdbcHelper.close(resultSet,preparedStatement);
        return loginLogs;
    }

    public Collection<LoginLog> findAllByUsername(List<Condition> conditionList, Connection conn) throws SQLException {
        ArrayList<LoginLog> loginLogs = new ArrayList<LoginLog>();
        //预编译方式进行查询
        StringBuilder sql = new StringBuilder("SELECT id FROM user") ;
        if(conditionList!=null){
            String whereClause = Condition.toWhereClause(conditionList);
            sql.append(whereClause);
        }
        String s = "select * from LoginLog WHERE user_id IN(" +
                sql.toString() + ")";
        PreparedStatement preparedStatement = conn.prepareStatement(s);
        ResultSet resultSet = preparedStatement.executeQuery();
        //将查询到的数据存到departments集合中
        while (resultSet.next()){
            int id = resultSet.getInt(1);
            int user_id = resultSet.getInt(2);
            String IP = resultSet.getString(3);
            String loginLocation = resultSet.getString(4);
            String clientSystem = resultSet.getString(5);
            String HTTPProtocol = resultSet.getString(6);
            String visitBrowser = resultSet.getString(7);
            Timestamp timestamp = resultSet.getTimestamp(8);
            User user = UserDao.getInstance().find(user_id, conn);
            System.out.println(user);
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LoginLog loginLog = new LoginLog(id,user,IP,loginLocation,clientSystem,HTTPProtocol,visitBrowser,
                    timestamp.toLocalDateTime().format(dtf));
            loginLogs.add(loginLog);
        }
        JdbcHelper.close(resultSet,preparedStatement);
        return loginLogs;
    }

    public LoginLog find(Integer id, Connection conn) throws SQLException {
        //sql语句
        String find_sql = "SELECT * FROM LoginLog where id = ? ";
        PreparedStatement pstmt = conn.prepareStatement(find_sql);
        pstmt.setInt(1,id);
        ResultSet resultSet = pstmt.executeQuery();
        LoginLog loginLog = null;
        if (resultSet.next()) {
            int user_id = resultSet.getInt(2);
            String IP = resultSet.getString(3);
            String loginLocation = resultSet.getString(4);
            String clientSystem = resultSet.getString(5);
            String HTTPProtocol = resultSet.getString(6);
            String visitBrowser = resultSet.getString(7);
            Timestamp timestamp = resultSet.getTimestamp(8);
            User user = UserDao.getInstance().find(user_id, conn);
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            loginLog = new LoginLog(id,user,IP,loginLocation,clientSystem,HTTPProtocol,visitBrowser,
                    timestamp.toLocalDateTime().format(dtf));
        }
        JdbcHelper.close(pstmt);
        return loginLog;
    }
    public boolean add(LoginLog loginLog, Connection conn) throws SQLException {
        //使用预编译创建SQL语句
        String create = "insert into LoginLog(user_id,ip,loginLocation,clientSystem,HTTPProtocol,VisitBrowser)" +
                " values (?,?,?,?,?,?)";
        PreparedStatement statement = conn.prepareStatement(create);
        //执行SQL语句并返回结果和关闭连接
        try {
            statement.setInt(1,loginLog.getUser().getId());
            statement.setString(2,loginLog.getIP());
            statement.setString(3,loginLog.getLoginLocation());
            statement.setString(4,loginLog.getClientSystem());
            statement.setString(5,loginLog.getHTTPProtocol());
            statement.setString(6,loginLog.getVisitBrowser());
            int rowsAffected = statement.executeUpdate();
            return rowsAffected > 0;
        }finally {
            JdbcHelper.close(statement);
        }
    }
}

